package org.taofuxn.framework.ui.view;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import org.taofuxn.framework.ui.activity.WebActivity;


public class FgWebChromeClient extends WebChromeClient implements View.OnClickListener {
    public static final int REQUEST_SELECT_FILE_5 = 5;//5.0
    public static final int REQUEST_CODE_PICK_IMAGE = 0;
    private static WebActivity context;
    public final int REQUEST_CODE_IMAGE_CAPTURE = 1;
    public ValueCallback<Uri> mUploadMessage;
    public ValueCallback<Uri[]> uploadMessageFive;
    private ProgressBar progressBar;

    public FgWebChromeClient(WebActivity context) {
        this.context = context;
    }

    public FgWebChromeClient(WebActivity webActivity, ProgressBar progressBar) {
        this.progressBar = progressBar;
        this.context = webActivity;
    }


    @Override
    public boolean onJsAlert(WebView view, String url, String message,
                             JsResult result) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("对话框").setMessage(message)
                .setPositiveButton("确定", null);
        builder.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
                return true;
            }
        });
        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
        result.confirm();//因为没有绑定事件，需要强行confirm

        return true;
    }

    @Override
    public boolean onJsConfirm(WebView view, String url, String message,
                               final JsResult result) {
        // TODO Auto-generated method stub

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("对话框").setMessage(message).
                setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        result.confirm();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                result.cancel();
            }
        });

        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface arg0) {
                result.cancel();
            }
        });

        AlertDialog mdialog = builder.create();
        mdialog.show();
        result.cancel();

        return true;
    }
    /*------------------------------------------------------------------------------------------*/

    @Override
    public void onReceivedTitle(WebView view, String title) {
        Log.i("AAA", "title=" + title);
        String[] strs = title.split(" ");
        String theme = "";
        for (String str : strs) {
            if (!TextUtils.isEmpty(str)) {
                theme = str;
                break;
            }
        }
        if (theme.length() > 12)
            theme = theme.substring(0, 9) + "...";
        context.setTitle(theme);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        progressBar.setProgress(newProgress);

        super.onProgressChanged(view, newProgress);
    }

    // For Android = 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        mUploadMessage = uploadMsg;
//        showAlbumDialog(context);
    }

    //For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        openFileChooser(uploadMsg);
    }
/*------------------------------------------------------------------------------------*/

    // For Android  > 4.1+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        openFileChooser(uploadMsg);
    }

    /* For Android 5.0+*/
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        uploadMessageFive = filePathCallback;
        return true;
    }



    @Override
    public void onClick(View v) {
        context.finish();
    }
}
